package com.bdy.lm.taximanager.thread;

import com.bdy.lm.taximanager.http.HttpJson;
import com.bdy.lm.taximanager.model.Address;
import com.bdy.lm.taximanager.model.JuAddress;
import com.bdy.lm.taximanager.model.StandardRoute;
import com.google.gson.Gson;

/**
 * Created by Administrator on 2017/4/29 0029.
 */

public class GetAddress extends Thread{

    private StandardRoute sRoute;
    private HttpJson httpJson = new HttpJson();
    private Finish finish;

    //private String juUrl = "http://apis.juhe.cn/geo/?lng=%f&lat=%f&type=1&dtype=json&key=dcc1628859e2bd3876b05d76652af913";

    public GetAddress(StandardRoute sRoute, Finish finish) {
        this.sRoute = sRoute;
        this.finish = finish;
    }

    @Override
    public void run() {
        String key = "AqXyGhkP8WLDG0ogeLFdPeK5z6uVyZZs";
        String url = "http://api.map.baidu.com/geocoder/v2/?ak=%s&location=%f,%f&output=json";
        String urlA = String .format(url, key, sRoute.getAlatitude(), sRoute.getAlongitude());
        String urlB = String .format(url, key, sRoute.getBlatitude(), sRoute.getBlongtitde());

        //String juUrlA = String.format(juUrl, sRoute.getAlongitude(), sRoute.getAlatitude());
        //String juUrlB = String.format(juUrl, sRoute.getBlongtitde(), sRoute.getBlatitude());

        //System.out.println("urlJu:" + juUrlA + juUrlB);
//        double dA = getDistance(latitude, longitude, sRoute.getAlatitude(), sRoute.getAlongitude());
//        double dB = getDistance(latitude, longitude, sRoute.getBlatitude(), sRoute.getBlongtitde());
//        if (dA > dB) {
//            url = String .format(ur, key, sRoute.getAlatitude(), sRoute.getAlongitude());
//        } else {
//            url = String .format(ur, key, sRoute.getBlatitude(), sRoute.getBlongtitde());
//        }

        //调用百度地图接口
        finish.setResult(getAddress(urlA), getAddress(urlB));

        //调用聚合数据接口
        //finish.getResult(getAddrJu(juUrlA), getAddrJu(juUrlB));
    }

    /**
     *  采用百度地图接口解析经纬度
     * @param url
     * @return
     */
    private String getAddress(String url) {
        System.out.println("urlget"+url);
        String addr;
        while(true) {
            if (httpJson.getGetJson() == null) {
                httpJson.HttpJsonGet(url);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                addr = new Gson().fromJson(httpJson.getGetJson().toString(), Address.class).getResult().getFormatted_address();
                System.out.println("urladdr"+addr);
                break;
            }
        }
        return addr;
    }

    /**
     *  采用聚合数据接口解析经纬度
     * @param url
     * @return
     */
    private String getAddrJu(String url) {
        System.out.println("urlget"+url);
        String addr;
        while(true) {
            if (httpJson.getGetJson() == null) {
                httpJson.HttpJsonGet(url);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                addr = new Gson().fromJson(httpJson.getGetJson().toString(), JuAddress.class).getResult().getAddress();
                System.out.println("urladdr"+addr);
                break;
            }
        }
        return addr;
    }

    public interface Finish {
        void setResult(String Aaddress, String Baddress);
    }
    //public interface
}